home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
Alpha
/
Tcl
/
SystemCode
/
grep.tcl
< prev
next >
Wrap
Text File
|
1997-04-04
|
4KB
|
142 lines
#================================================================================
# 'greplist' and 'grepfset' are used for batch searching from the "find" dialog.
# Hence, you really shouldn't mess with them unless you know what you are doing.
#================================================================================
proc greplist {args} {
global tileLeft tileTop tileWidth tileHeight errorHeight
set recurse [car $args]
set word [cadr $args]
set args [cddr $args]
set num [expr [llength $args] - 2]
set exp [lindex $args $num]
set arglist [lindex $args [expr $num + 1]]
set opened 0
set owin 0
set cid [scancontext create]
set cmd [lrange $args 0 [expr $num - 1]]
eval scanmatch $cmd {$cid $exp {
if {!$word || [regexp -nocase "(^|\[^a-zA-Z0-9\])${exp}(\[^a-zA-Z0-9\]|\$)" $matchInfo(line)]} {
if (!$owin) {
set owin 1
new -n {* Batch Find *} -g $tileLeft $tileTop $tileWidth $errorHeight
global winModes
set name [lindex [winNames] 0]
changeMode [set winModes($name) Brws]
insertText "(<cr> to go to match)\r-----\r"
set w [car [winNames -f]]
set opened 1
}
set l [expr 20 - [string length [file tail $f]]]
insertText -w $w "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$f\r"}
}
}
foreach f $arglist {
message [file tail $f]
if {![catch {set fid [open $f]}]} {
scanfile $cid $fid
close $fid
}
}
scancontext delete $cid
if {$opened} {
select [nextLineStart [nextLineStart 0]] [nextLineStart [nextLineStart [nextLineStart 0]]]
setWinInfo dirty 0
setWinInfo read-only 1
}
message ""
}
proc grepfset {args} {
set num [expr [llength $args] - 2]
set exp [lindex $args $num]
set fset [lindex $args [expr $num + 1]]
eval greplist 0 [lrange $args 0 [expr $num-1]] {$exp [getFileSet $fset]}
}
proc grep {exp args} {
set files {}
foreach arg $args {
if {![catch {glob -t TEXT $arg} lst]} {
append files " " $lst
}
# if {![catch {glob -t ttro $arg} lst]} {
# append files " " $lst
# }
}
if {$files==""} {return $lst}
set cid [scancontext create]
scanmatch $cid $exp {
if {!$blah} {
set blah 1
set lines "(<cr> to go to match)\r"
}
set l [expr 20 - [string length [file tail $f]]]
append lines "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$f\r"
}
set blah 0
set lines ""
foreach f $files {
if {![catch {set fid [open $f]}]} {
message [file tail $f]
scanfile $cid $fid
close $fid
}
}
scancontext delete $cid
return [string trimright $lines "\r"]
}
#===============================================================================
set lastMatchingLines ""
proc matchingLines {{reg ""} {for 1} {ign 1} {word 0} {regexp 1}} {
global lastMatchingLines tileLeft tileTop tileWidth errorHeight
if {![string length $reg] && [catch {prompt "Regular expression:" $lastMatchingLines} reg]} return
set lastMatchingLines $reg
if {![string length $reg]} return
if {!$regexp} {
set reg [quoteExpr2 $reg]
regsub -all {\\\\} $reg {\\} reg
}
if $word {
set reg "^.*\\b$reg\\b.*$"
} else {
set reg "^.*$reg.*$"
}
set pos [expr $for ? 0 : [getPos]]
set fileName [car [winNames -f]]
set matches 0
set lines {}
while {![catch {search -s -f 1 -r 1 -i $ign $reg $pos} mtch]} {
append lines "\r" [format "Line %d: " [lindex [posToRowCol [lindex $mtch 0]] 0]] [eval getText $mtch] "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$fileName"
set pos [lindex $mtch 1]
incr matches
}
new -n {* Matching Lines *} -g $tileLeft $tileTop $tileWidth $errorHeight
insertText [format "%d matching lines (<cr> to go to match)\r-----" $matches] $lines "\r"
select [nextLineStart [nextLineStart 0]] [nextLineStart [nextLineStart [nextLineStart 0]]]
global winModes
set name [lindex [winNames] 0]
changeMode [set winModes($name) Brws]
setWinInfo dirty 0
setWinInfo read-only 1
}
proc findBatch {forward ignore regexp word pat} {
matchingLines $pat $forward $ignore $word $regexp
}